package 哈夫曼编码;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class 哈夫曼编码3 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNextLine()) {
			String content = scanner.nextLine();
			char[] chs = content.toCharArray();
			int n = chs.length;
			PriorityQueue<Integer> intque = new PriorityQueue<Integer>();

			Arrays.sort(chs);
			int j, cnt = 0;
			for (int i = 0; i < n;) {
				j = i;
				while (j < n && chs[i] == chs[j])
					j++;
				intque.add(j - i);
				i = j;
				cnt++;
			}
			int ret = 0;
			for (int i = 0; i < cnt - 1; i++) {
				int a = intque.remove();
				int b = intque.remove();
				ret += a + b;
				intque.add(a + b);
			}
			
			// 按哈夫曼树编码后字段长度本质上等于非叶子节点的权值和
			System.out.println(ret);
		}
		scanner.close();

	}

}